package org.wangnian.demo03_springboot_mail.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.wangnian.demo03_springboot_mail.model.Emp;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

@Service
public class MailPushService {

    private static final Logger logger = LoggerFactory.getLogger(MailPushService.class);

    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    MailProperties mailProperties;//用来获取 mail 的发送人
    @Autowired
    TemplateEngine templateEngine;// thymeleaf 模板引擎

    public void sendMimeMessage(Emp emp) {
        sendMimeMessage(emp, null);
    }

    // 发送一个 Html 形式的邮件
    public void sendMimeMessage(Emp emp, File attachment) {

        try {
            // MimeMessageHelper支持发送复杂邮件模板，支持文本、附件、HTML、图片等
            MimeMessage msg = javaMailSender.createMimeMessage();
            // boolean multipart：是否支持发送附件
            MimeMessageHelper helper = new MimeMessageHelper(msg, true);

            helper.setFrom(mailProperties.getUsername());//设置发送人
            helper.setTo(emp.getEmail());//设置收件人
            helper.setSubject("入职欢迎");//设置主题
            helper.setSentDate(new Date());//设置发送日期
            // 渲染 mail.html 文件为 字符串
            Context context = new Context();
            context.setVariable("name", emp.getName());
            context.setVariable("postName", emp.getPostName());
            context.setVariable("jobLevelName", emp.getJobLevelName());
            context.setVariable("departmentName", emp.getDepartmentName());
            String mail = templateEngine.process("mail", context);// thymeleaf 渲染 html 页面
            helper.setText(mail, true);//设置发送内容为 html 文本
            // 设置附件
            if (attachment != null) {
                helper.addAttachment(attachment.getName(), attachment);
            }
            javaMailSender.send(msg);//发送
            logger.error("邮件发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("邮件发送失败：" + e.getMessage());
        }
    }

    // 发送一个简单邮件
    public void sendSimpleMessage() {
        try {
            SimpleMailMessage simpleMessage = new SimpleMailMessage();
            // 配置发送者邮箱
            simpleMessage.setFrom(mailProperties.getUsername());
            // 配置接受者邮箱
            simpleMessage.setTo("15271722359@163.com");
            // 配置邮件主题
            simpleMessage.setSubject("主题：简单邮件");
            // 配置邮件内容
            simpleMessage.setText("测试邮件内容5");
            //邮件发送时间
            simpleMessage.setSentDate(new Date());
            // 发送邮件
            javaMailSender.send(simpleMessage);
            logger.error("邮件发送成功");
        } catch (MailException e) {
            e.printStackTrace();
            logger.error("邮件发送失败：" + e.getMessage());
        }
    }


}
